
package com.wyzz.hr.action;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IRecruitapplierBo;
import com.wyzz.hr.pojo.Recruitapplier;

public class DownFile extends BaseAction {

	public DownFile() {
		docStream = null;
		contentDisposition = null;
	}

	public String downProfile() {
		String forward = null;
		try {
			forward = downLoadFile(fileName, fileName);
		} catch (Exception e) {
			forward = null;
		}
		return forward;
	}

	public String downLoadFile(String fileName, String downName) {
		docStream = FileOperate.getFileInputStream("sys.profile.file.path",
				fileName);
		if (id != null && !id.equals("")) {
			IRecruitapplierBo recruitapplierBo = (IRecruitapplierBo) getBean("applierBo");
			Recruitapplier tempApplier = recruitapplierBo.loadApplierById(id);
			String str = (new StringBuilder())
					.append(tempApplier.getRecaName()).append("(").append(
							tempApplier.getRecaPhone()).append(")").toString();
			if (tempApplier.getRecaChannelId() != null)
				str = (new StringBuilder()).append(str).append("_").append(
						tempApplier.getRecaChannelId()).toString();
			downName = downName.substring(downName.indexOf("."));
			str = (new StringBuilder()).append(str).append(downName).toString();
			str = (new StringBuilder()).append("filename=\"").append(str)
					.append("\"").toString();
			try {
				contentDisposition = new String(str.getBytes("utf-8"),
						"iso-8859-1");
			} catch (Exception e) {
			}
		} else {
			contentDisposition = "attachment;filename=\""+fileName+"\"";
		}
		if (docStream == null)
			return "fileError";
		else
			return "download";
	}

	public String deleteApplierAttach(String applierId, String fileName,
			HttpSession session) {
		if (session == null || WebContextFactory.get() == null
				|| WebContextFactory.get().getSession() == null)
			return null;
		String auth = DWRUtil.checkAuth("deleteapplier", "execute");
		if (!"HR".equals(auth))
			return "ERROR1";
		IRecruitapplierBo recruitapplierBo = (IRecruitapplierBo) getBean("applierBo");
		Recruitapplier tempRa = recruitapplierBo.loadApplierById(applierId);
		List applierList = new ArrayList();
		if (tempRa != null) {
			if (fileName.equals(tempRa.getRecaResume1()))
				tempRa.setRecaResume1(null);
			else
				tempRa.setRecaResume2(null);
			applierList.add(tempRa);
			recruitapplierBo.saveOrUpdateApplier(applierList);
			FileOperate.deleteFile("sys.recruitment.applier.path", fileName);
			return "SUCCESS";
		} else {
			return "ERROR2";
		}
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public FileInputStream getDocStream() {
		return docStream;
	}

	public void setDocStream(FileInputStream docStream) {
		this.docStream = docStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private FileInputStream docStream;
	private String contentDisposition;
	private String fileName;
	private String id;
}
